#include "dd_base_2d.h"
#include "util/mmgr.h"		// Needs to come in early


using namespace dd;

PrimitiveBase2D::PrimitiveBase2D()
{

}

PrimitiveBase2D::~PrimitiveBase2D()
{

}

void PrimitiveBase2D::AddVertex(DBG_BUFFER_2D* _pBuffer, const math::Vec2& _From, COLOR _Color)
{
	if (_pBuffer->CurrentVertC + 2 >= _pBuffer->MaxVertC)
	{
		FlushBuffer(_pBuffer);
	}
	_pBuffer->DataA[_pBuffer->CurrentVertC++].Fill(_From.x*_pBuffer->Width, _From.y*_pBuffer->Height, _Color, 0, 0);
}


void PrimitiveBase2D::AddLine(DBG_BUFFER_2D* _pBuffer, const math::Vec2& _From, const math::Vec2& _To, COLOR _Color)
{
	if (_pBuffer->CurrentVertC + 2 >= _pBuffer->MaxVertC)
	{
		FlushBuffer(_pBuffer);
	}
	_pBuffer->DataA[_pBuffer->CurrentVertC++].Fill(_From.x*_pBuffer->Width, _From.y*_pBuffer->Height, _Color, 0, 0);
	_pBuffer->DataA[_pBuffer->CurrentVertC++].Fill(_To.x*_pBuffer->Width, _To.y*_pBuffer->Height, _Color, 0, 0);
}

void PrimitiveBase2D::FlushBuffer(DBG_BUFFER_2D* _pBuffer)
{
	if (_pBuffer->CurrentVertC==0)
		return;	// no need to render.. nothing here

	// render
	_pBuffer->pGraphics->DrawVertex2D(_pBuffer->DataA, _pBuffer->CurrentVertC, Graphics::PT_LINE_LIST);

	// reset
    _pBuffer->CurrentVertC = 0;	
}

void PrimitiveBase2D::FlushBufferTri(DBG_BUFFER_2D* _pBuffer)
{
	if (_pBuffer->CurrentVertC==0)
		return;	// no need to render.. nothing here

	// render
	_pBuffer->pGraphics->DrawVertex2D(_pBuffer->DataA, _pBuffer->CurrentVertC, Graphics::PT_TRI_LIST);

	// reset
	memset(_pBuffer->DataA, 0, sizeof(Graphics::Vertex2D) * _pBuffer->MaxVertC);
    _pBuffer->CurrentVertC = 0;	
}
